﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{

    public class HomeController : BaseController
    {
        //
        // GET: /Home/

        [StatFilter]
        [UrlStatFilter]
        [UserAuth(UserType=UserTypeEnum.Student)]
        public ActionResult Index()
        {
            if (UserHelper.LoginUser == null)
                return RedirectToAction("Logon", "Account");
            return View();
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult StudentViewUc(int id)
        {
            var info = StudentBLL.Get(id);
            if (info == null)
                return NotFound();
            ViewData.Model = info;
            return PartialView();
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult NewsListUc(int? pid, string search, string startDate, string endDate)
        {
            CommonRequest.ClearCache();
            var list = CourseNoticeBLL.GetList(search, startDate.CheckNoDatetime(), endDate.CheckNoDatetime()).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, 9);
            return PartialView(model);
        }

        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult TeacherListUc()
        {
            CommonRequest.ClearCache();
            var list = TeacherBLL.GetAllTeacher().AsQueryable();
            var model = list.ToPagedList(1, 3);
            return PartialView(model);
        }

       [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult ResourcesListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonResourcesBLL.GetList().OrderByDescending(o => o.Hits);
            var model = list.ToPagedList(pid ?? 1, 9);
            return PartialView("ResourcesListUc", model);
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult QuestionListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = QuestionBLL.GetList(null, null, true).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, 17);
            return PartialView(model);
        }
        [UserAuth(UserType = UserTypeEnum.Student)]
        public ActionResult VideoListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CommonResourcesBLL.GetList().Where(o => o.Type == 5).OrderByDescending(o => o.Hits);
            var model = list.ToPagedList(pid ?? 1, 4);
            return PartialView(model);
        }

    }
}
